
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios';


const app = createApp(App)

app.use(router)
app.use(store)

app.mount('#app')



// 创建axios实例
const apiClient = axios.create({
  baseURL: 'http://localhost:3000',
  withCredentials: false,
  headers: {
    Accept: 'application/json',
    'Content-Type': 'application/json'
  }
});

// 请求拦截器
apiClient.interceptors.request.use(config => {
  const token = localStorage.getItem('token');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

// 响应拦截器
apiClient.interceptors.response.use(
  response => response,
  error => {
    if (error.response && error.response.status === 401) {
      // 401未授权，清除token并跳转到登录页
      store.dispatch('logout');
      router.push('/');
    }
    return Promise.reject(error);
  }
);

// 挂载到全局
app.config.globalProperties.$axios = apiClient;
